package org.jeecg.modules.config;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * @author gjs
 * @description RestTemplate 统一添加 access_token 拦截器
 */
@Component
public class HeaderRequestInterceptor implements ClientHttpRequestInterceptor {
    @Override
    public ClientHttpResponse intercept(HttpRequest httpRequest, byte[] body, ClientHttpRequestExecution clientHttpRequestExecution) throws IOException {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
        if (HttpMethod.OPTIONS.toString().equals(request.getMethod()))
            clientHttpRequestExecution.execute(httpRequest, body);
        String auth = request.getHeader("Authorization");
        String xtoken = request.getHeader("X-Access-Token");
        String accessToken = request.getParameter("access_token");
        List<String> authorizations = httpRequest.getHeaders().get("Authorization");
        String authorization = "";
        if (accessToken != null){
            authorization = "bearer " + accessToken;
        }
        else if (auth != null){
            authorization = auth;
        }else if (authorizations != null){
            authorization = "bearer " + authorizations.get(0);
        }
        HttpHeaders headers = httpRequest.getHeaders();
        headers.add("Authorization", authorization);
        if(xtoken != null)
            headers.add("X-Access-Token",xtoken);
        return clientHttpRequestExecution.execute(httpRequest, body);
    }

}
